package com.htht.smartschool.action.course;

import java.util.List;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.htht.smartschool.action.baseaction.BaseAction;
import com.htht.smartschool.common.model.ParamData;
import com.htht.smartschool.common.servlet.DataContainer;
import com.htht.smartschool.model.course.CourseData;
import com.htht.smartschool.model.course.TCourse;
import com.htht.smartschool.model.course.TCourseorder;
import com.htht.smartschool.model.course.TCoursesubject;
import com.htht.smartschool.service.course.CourseService;
import com.htht.smartschool.util.system.GsonUtils;
import com.htht.smartschool.util.system.ResultSetToExcel;

public class CourseAction extends BaseAction {

	@SuppressWarnings("rawtypes")
	@Override
	public boolean processData(DataContainer obj, ParamData pd) {
		boolean flag = false;
		CourseService courseService = new CourseService();
		TCourse tCourse = null;
		TCoursesubject tCoursesubject = null;
		try {
			tCourse = (TCourse) GsonUtils.getInstance().fromJson(pd.getData(), TCourse.class);
			tCoursesubject = (TCoursesubject) GsonUtils.getInstance().fromJson(pd.getData(), TCoursesubject.class);
		} catch (Exception e) {
		}
		if(pd.getCmd().equalsIgnoreCase("getCourseByStudentCode")){
			Map map = courseService.getCourseByStudentCode(tCourse);
			if(map != null && map.keySet().size() > 0){
				CourseData cd = (CourseData) map.get("result");
				ResultSetToExcel rse = (ResultSetToExcel) map.get("remark");
				if(cd != null){
					flag = true;
					if(rse != null){
						obj.setRemark(rse);
					}
					obj.setData(cd);
				}else{
					obj.setData("查无数据！");
				}
			}else{
				obj.setData("查无数据！");
			}
			
		}else if(pd.getCmd().equalsIgnoreCase("getAllCourseSubject")){
			String type = tCoursesubject.getType();
			String schoolcode =  tCoursesubject.getSchoolcode();
			List<TCoursesubject> list = courseService.selectAllCourseSubject(type,schoolcode);
			if(list.size() > 0){
				flag = true;
				obj.setData(list);
			}else{
				obj.setData("查无数据！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addOrUpdate")){
			List<TCourse> courseList = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TCourse>>(){}.getType());
			List<TCourse> result  = courseService.addOrUpdate(courseList);
			if(result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getCoursePlan")){
			List result  = courseService.getCoursePlan(tCourse.getClasscode());
			if(result.size() > 0){
				flag = true;
				obj.setData(result);
			}else{
				obj.setData("添加失败！");
			}

		}else if(pd.getCmd().equalsIgnoreCase("addTeahcerForCourse")){
			List<TCourseorder> courseOrderList = GsonUtils.getInstance().gson.fromJson(pd.getData(),  new TypeToken<List<TCourseorder>>(){}.getType());
			flag = courseService.addTeahcerForCourse(courseOrderList);
			if(flag){
				obj.setData(true);
			}else{
				obj.setData("绑定失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("addCourseSubjectCode")){
			flag = courseService.addCourseSubjectCode(tCoursesubject);
			if(flag){
				obj.setData(true);
			}else{
				obj.setData("增加失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("delCourseSubjectCode")){
			flag = courseService.delCourseSubjectCode(tCoursesubject);
			if(flag){
				obj.setData(true);
			}else{
				obj.setData("删除失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("updateTeahcerForCourse")){
			flag = courseService.updateTeahcerForCourse(tCoursesubject);
			if(flag){
				obj.setData(true);
			}else{
				obj.setData("修改失败！");
			}
		}else if(pd.getCmd().equalsIgnoreCase("getTeahcerForCourse")){
			Map map = courseService.getTeahcerForCourse(tCoursesubject);
			if(map != null){
				flag = true;
				obj.setData(map);
			}else{
				obj.setData("查询失败！");
			}
		}
		return flag;
	}

}
